﻿using Orchard.CounterField.Settings;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
using Orchard.CounterField.ViewModels;
using Orchard.Localization;

namespace Orchard.CounterField.Drivers {
    public class CounterFieldDriver : ContentFieldDriver<Orchard.CounterField.Fields.CounterField>
    {
        public IOrchardServices Services { get; set; }
        private const string TemplateName = "Fields/Counter.Edit";

        public CounterFieldDriver(IOrchardServices services)
        {
            Services = services;
            T = NullLocalizer.Instance;
        }

        public Localizer T { get; set; }

        private static string GetPrefix(ContentField field, ContentPart part) {
            return part.PartDefinition.Name + "." + field.Name;
        }

        private static string GetDifferentiator(Orchard.CounterField.Fields.CounterField field, ContentPart part)
        {
            return field.Name;
        }

        protected override DriverResult Display(ContentPart part, Orchard.CounterField.Fields.CounterField field, string displayType, dynamic shapeHelper)
        {
            var settings = field.PartFieldDefinition.Settings.GetModel<CounterFieldSettings>();
            var viewModel = new CounterFieldViewModel
            {
                Field= field,
                Settings = settings, 
                FormattedStart = field.StartValue,
                FormattedStop = field.StopValue
            };

            //if (settings.Type == CounterType.Date) {
            //    string start = DateTime.Now.ToString("yyyy:MM:dd");
            //    string stop = DateTime.Now.ToString("yyyy:MM:dd");
            //    settings.Format = "9999:12:31";
            //    viewModel.Settings = settings;

            //    if(field.PickStartDate) {
            //        start = FormatDate(field.StartValue);
            //    }
            //    if (field.PickStopDate)
            //    {
            //        stop = FormatDate(field.StopValue);
            //    }

            //    viewModel.FormattedStart = start;
            //    viewModel.FormattedStop = stop;
            //}

            return ContentShape("Fields_Counter", GetDifferentiator(field, part), () =>
                    shapeHelper.Fields_Counter(Model: viewModel));



        }

        protected override DriverResult Editor(ContentPart part, Orchard.CounterField.Fields.CounterField field, dynamic shapeHelper) {
            return ContentShape("Fields_Counter_Edit", GetDifferentiator(field, part),
                () => shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: field, Prefix: GetPrefix(field, part)));
        }

        protected override DriverResult Editor(ContentPart part, Orchard.CounterField.Fields.CounterField field, IUpdateModel updater, dynamic shapeHelper)
        {
            if (updater.TryUpdateModel(field, GetPrefix(field, part), null, null)) {
                var settings = field.PartFieldDefinition.Settings.GetModel<CounterFieldSettings>();

                if (settings.Required && (string.IsNullOrWhiteSpace(field.StartValue) && string.IsNullOrWhiteSpace(field.StopValue)))
                {
                    updater.AddModelError(GetPrefix(field, part), T("The field {0} is mandatory.", T(field.DisplayName)));
                }

                //Date validation
                //if(settings.Type == CounterType.Date) {
                //    if (!string.IsNullOrWhiteSpace(field.StartValue) && field.StartValue.Length >= 9)
                //    {
                //        if (FormatDate(field.StartValue) == null)
                //        {
                //            updater.AddModelError(GetPrefix(field, part), T("Start value must be a valid date."));
                //        }
                //    }

                //    if (!string.IsNullOrWhiteSpace(field.StopValue) && field.StopValue.Length >= 9)
                //    {
                //        if (FormatDate(field.StopValue) == null)
                //        {
                //            updater.AddModelError(GetPrefix(field, part), T("Stop value must be a valid date."));
                //        }
                //    }                
                //}
            }

            return Editor(part, field, shapeHelper);
        }


        protected override void Describe(DescribeMembersContext context) {
            context
                .Member(null, typeof(string), T("Value"), T("The value of the field."));
        }

        //private string FormatDate(string date) {
        //    DateTime value;
        //    if (!DateTime.TryParse(date, out value))
        //    {
        //        return null;
        //    }

        //    return value.ToString("yyyy:MM:dd");
        //}
    }
}
